package org.nudt.video.controller

import cn.dev33.satoken.annotation.SaCheckLogin
import cn.dev33.satoken.stp.StpUtil
import org.nudt.common.model.ResponseData
import org.nudt.common.model.User
import org.nudt.common.model.dto.UserDto
import org.nudt.video.service.UserService
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

@RestController
@RequestMapping("/v1/user")
class UserController(private val userService: UserService) {
    
    /**
     * app登录
     */
    @PostMapping("/login")
    fun appLogin(@RequestBody userDto: UserDto): ResponseData<HashMap<String, Any>> {
        val user = userService.appLogin(userDto)
        StpUtil.login(userDto.appid)
        // 创建一个HashMap用于存储返回的数据
        val resultMap = HashMap<String, Any>()
        // 将token值放入resultMap中
        resultMap["token"] = StpUtil.getTokenValue()
        // 将管理员信息放入resultMap中
        resultMap["user"] = user
        return ResponseData.success(resultMap)
    }
    
    /**
     * 修改app用户头像
     * @param file 头像文件
     */
    @SaCheckLogin
    @PostMapping("/avatar/edit")
    fun updateAvatar(@RequestParam avatar: MultipartFile): ResponseData<User> {
        val appid = StpUtil.getLoginIdAsString()
        return ResponseData.success(userService.updateAvatar(appid, avatar))
    }
    
    /**
     * 修改app用户信息
     * @param nickname 昵称
     * @param gender 性别
     */
    @SaCheckLogin
    @PostMapping("/edit")
    fun updateInformation(
        @RequestParam(required = false) nickname: String?,
        @RequestParam(required = false) gender: Int?,
    ): ResponseData<User> {
        val appid = StpUtil.getLoginIdAsString()
        return ResponseData.success(userService.updateUserInfo(appid, nickname, gender))
    }
}